package com.threadmethod.synchronize;

/**
 * @PackageName:com.threadmethod.synchronize
 * @ClassName:Test13
 * @Description: 同步方法与同步代码块如何选择
 *                同步方法 锁的粒度粗 并发效率低
 * @Author: zhangxiaoyu
 * @date 2020/12/9 17:15
 */
public class Test13 {
    
    public static void main(String[] args){

        Test13 obj = new Test13();

        new Thread(new Runnable() {
            @Override
            public void run() {
              obj.doLongTimeTask();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                obj.doLongTimeTask();
            }
        }).start();

    }
    
    public synchronized  void doLongTimeTask(){
        System.out.println("task begin!!");
        try {

            Thread.sleep(3000);
            

                System.out.println("开始同步！！！");
                for(int i=1;i<=100;i++){
                    System.out.println(Thread.currentThread().getName()+"--->"+i);
                }


            System.out.println("task begin");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
